<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="试卷名称">
        <a-input placeholder="试卷名称" v-decorator="['name', {rules: [{required: true, message: '请输入试卷名称'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="试卷总分">
        <a-input placeholder="试卷总分" v-decorator="['mark', {rules: [{required: true, message: '请输入试卷总分'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="学校类型">
        <a-select placeholder="请选择学校类型" v-decorator="['schoolType', {rules: [{required: true, message: '请选择学校类型'}]"
                  allowClear>
          <a-select-option v-for="(b, index) in schoolTypeList" :key="index" :value="b.dictValue">{{ b.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="年级id">
        <a-input placeholder="年级id" v-decorator="['gradeInfoId', {rules: [{required: true, message: '请输入年级id'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属科目id">
        <a-input placeholder="所属科目id" v-decorator="['subjectId', {rules: [{required: true, message: '请输入所属科目id'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
        <a-input placeholder="排序" v-decorator="['sort', {rules: [{required: true, message: '请输入排序'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试时间">
        <a-input placeholder="考试时间" v-decorator="['examTime', {rules: [{required: true, message: '请输入考试时间'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="考试人数">
        <a-input placeholder="考试人数" v-decorator="['examNumber', {rules: [{required: true, message: '请输入考试人数'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="已批改试卷数量">
        <a-input placeholder="已批改试卷数量" v-decorator="['correctNumber']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否发布">
        <a-input placeholder="是否发布" v-decorator="['publishFlag', {rules: [{required: true, message: '请输入是否发布'}]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="试题数量">
        <a-input placeholder="试题数量" v-decorator="['questionNumber', {rules: [{required: true, message: '请输入试题数量'}]}]" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveEPaperInfo } from '@/api/ePaperInfo'
import pick from 'lodash.pick'

export default {
  name: 'EPaperInfoModal',
  props: {
    schoolTypeList: {
      type: Array,
      required: true
    }
  },
  components: {},
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate() {
  },
  created() {
  },
  methods: {
    add() {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit(record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'name', 'createDate', 'updateDate', 'remark', 'mark', 'schoolType', 'gradeInfoId', 'subjectId', 'sort', 'examTime', 'examNumber', 'correctNumber', 'publishFlag', 'publishTime', 'questionNumber'))
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveEPaperInfo(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
